package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.sife_secondactivity.OneSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.ThreeSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.TwoSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.WebViewActivity;
import com.wangtao.firstapp.modles.OriginalAuthorCollect;

import java.util.List;

/**
 * 用户作者  介绍页面（收藏—>文章）
 * Created by lanouhn on 16/9/8.
 */
public class AuthorCollectTwoAdapter extends RecyclerView.Adapter<AuthorCollectTwoAdapter.CollectEssayViewHolder> {

    private Context context;
    private List<OriginalAuthorCollect.DataBean.TopicBean> topicBean;

    public AuthorCollectTwoAdapter(Context context, List<OriginalAuthorCollect.DataBean.TopicBean> topicBean) {
        this.context = context;
        this.topicBean = topicBean;
    }

    @Override
    public CollectEssayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_colecton_essay, null);
        CollectEssayViewHolder holder = new CollectEssayViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(CollectEssayViewHolder holder, final int position) {

        if (topicBean.size() > 0) {
            holder.ltOne.setVisibility(View.VISIBLE);
        }

        holder.ltTwo.setVisibility(View.VISIBLE);
        holder.title.setText(topicBean.get(position).getTitle());

        if (topicBean.get(position)
                .getType().equals("") && topicBean.get(position)
                .getType_id().equals("")) {
            holder.likes.setVisibility(View.VISIBLE);
            holder.likesView.setVisibility(View.VISIBLE);
            holder.likes.setText(topicBean.get(position).getLikes());
        }
        if (topicBean.get(position)
                .getType().equals("5") && topicBean.get(position)
                .getType_id().equals("")||topicBean.get(position)
                .getType().equals("2") && topicBean.get(position)
                .getType_id().equals("1")||topicBean.get(position)
                .getType().equals("2")&&topicBean.get(position)
                .getType_id().equals("")) {

            holder.views.setVisibility(View.VISIBLE);
            holder.viewsView.setVisibility(View.VISIBLE);
            holder.views.setText(topicBean.get(position).getViews());
        }


        holder.collecton.setText(topicBean.get(position).getDatestr() + " 收藏");

        if (topicBean.get(position).getPics().size() > 0) {
            Picasso.with(context).load(topicBean.get(position).getPics().get(0).getUrl()).into(holder.photo);
        } else {
            Picasso.with(context).load(topicBean.get(position).getPic()).into(holder.photo);
        }

        // }

        //整个Item的点击事件，点击后进入点击的Item页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取页面ID
                String item_ID = topicBean.get(position).getId();

                //type为空，type_id为空。  跳转至，小糖君有图页面
                if (topicBean.get(position).getType().equals("") && topicBean.get(position).getType_id().equals("")) {
                    Intent intent = new Intent(context, TwoSifeSecondActivity.class);
                    //传递ID到将要打开的页面
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //type为2，type_id为空。  跳转至，WebView页面
                if (topicBean.get(position).getType().equals("2")&&topicBean.get(position).getType_id().equals("")){
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //type为2，type_id为1。  跳转至，作者原创页面
                if (topicBean.get(position).getType().equals("2") && topicBean.get(position).getType_id().equals("1")) {
                    Intent intent = new Intent(context, OneSifeSecondActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }

                //type为5，type_id为空。  跳转至，小糖君无图页面
                if (topicBean.get(position).getType().equals("5") && topicBean.get(position).getType_id().equals("")){
                    Intent intent = new Intent(context, ThreeSifeSecondActivity.class);
                    intent.putExtra("item_ID", item_ID);
                    context.startActivity(intent);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return topicBean.size();
    }

    public class CollectEssayViewHolder extends RecyclerView.ViewHolder {

        private TextView title, likes, collecton, views;
        private ImageView photo, likesView, viewsView;

        private LinearLayout ltOne, ltTwo;
        //private RelativeLayout ltThree;
        private View view;


        public CollectEssayViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title_item_fragment_colecton_essay);

            //喜欢
            likes = (TextView) itemView.findViewById(R.id.likes_item_fragment_colecton_essay);//数字
            likesView = (ImageView) itemView.findViewById(R.id.likesView_item_fragment_colecton_essay);//图
            //查看
            views = (TextView) itemView.findViewById(R.id.views_item_fragment_colecton_essay);//数字
            viewsView = (ImageView) itemView.findViewById(R.id.viewsView_item_fragment_colecton_essay);//图

            collecton = (TextView) itemView.findViewById(R.id.colecton_item_fragment_colecton_essay);
            photo = (ImageView) itemView.findViewById(R.id.photo_item_fragment_colecton_essay);

            ltOne = (LinearLayout) itemView.findViewById(R.id.ltOne_item_fragment_colecton_essay);
            ltTwo = (LinearLayout) itemView.findViewById(R.id.ltTwo_item_fragment_colecton_essay);
            // ltThree= (RelativeLayout) itemView.findViewById(R.id.ltThree_item_fragment_colecton_essay);
            view = itemView.findViewById(R.id.view_item_fragment_colecton_essay);
        }
    }
}
